﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using LevenBlog.Core.Install;
using LevenBlog.Core.Install.Operations;

namespace LevenBlog.Web.install {
    public partial class step2 : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            string q = Request.QueryString["q"];
            if (!String.IsNullOrEmpty(q) && q == "ajax") {
                string path = Request.QueryString["path"];
                Response.ContentType = "application/json";
                Response.Write(CheckFileSystem(path));
                Response.End();
            } else {
                int step = Request.GetSetup();
                if (step < 1)
                    Response.Redirect("step1.aspx");
                (Master as install).SubTitle = "第二步 - 安装目录权限检查";
            }
        }

        protected string CheckFileSystem(string path) {
            FileSystemCheck check = new FileSystemCheck();
            check.Data = path;
            InstallConfig config = InstallConfigService.Read();
            if (config.CheckPathList.Count(e => e == path) <= 0) {
                config.CheckPathList.Add(path);
            }
            InstallConfigService.Write(config);
            var result = check.Execute(new OperationContext());
            string message;
            if (result.Result) {
                message = String.Format("{0},检查路径\"{1}\"...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OK.<br />", check.Name, path);
            } else {
                message = String.Format("{0},检查路径\"{1}\"...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"checkerror\">错误:{2}</span><br />", check.Name, path, result.Errors.ToErrorString());
            }
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(new { result = result.Result, message = message });
        }

        protected void step2_pre_Click(object sender, EventArgs e) {
            Response.Redirect("step1.aspx");
        }

        protected void step2_next_Click(object sender, EventArgs e) {
            Response.SetSetup(2);
            Response.Redirect("step3.aspx");
        }
    }
}
